package com.umeox.watch.moto.gallery.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.umeox.watch.moto.gallery.R;

import java.util.List;


public class PhotoPreviewAdapter extends RecyclerView.Adapter<PhotoPreviewAdapter.ItemViewHolder> {

    private List<String> list;

    private OnItemLongClickListener mLongClickListener;


    public PhotoPreviewAdapter(List<String> list, OnItemLongClickListener mLongClickListener) {
        this.list = list;
        this.mLongClickListener = mLongClickListener;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_photo_preview, parent, false);

        return new ItemViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.bindData(list.get(position));
        holder.itemView.setOnClickListener(v -> mLongClickListener.onLongClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private ImageView mPhotoIv;


        public ItemViewHolder(View itemView) {
            super(itemView);
            mPhotoIv = itemView.findViewById(R.id.iv_photo);
        }

        public void bindData(String path) {
            Glide.with(itemView.getContext()).load(path).centerCrop().into(mPhotoIv);
        }
    }

    public interface OnItemLongClickListener {
        void onLongClick(int position);
    }
}

